//
//  VOCWordList.m
//  iVoc
//
//  Created by Wallace Wu on 09-3-11.
//  Copyright 2009 Sonic Solutions. All rights reserved.
//

#import "VOCWordList.h"

NSArray *allLists = nil;

@interface VOCWordList(private)
-(void)generateIndex;
@end

@implementation VOCWordList(private)

-(void)generateIndex
{
    //dummy implementation
    [index release];
    index = [[NSArray array] retain];
}

@end

@implementation VOCWordList

@dynamic wordCount;
@synthesize name;
@dynamic data;
@dynamic index;

-(NSUInteger)wordCount
{
    wordCount = [self.data count];
    return wordCount;
}

-(NSArray*)index
{
    if(!index || needsReindex)
    {
        [self generateIndex];
        needsReindex = NO;
    }
    return index;
}

-(NSArray*)data
{
    return data;
}

-(VOCWORDLanguage)explanationLanguages
{
    VOCWORDLanguage result = VOC_WORD_LANG_UNKNOWN;
    for (VOCWordData *word in self.data) {
        result |= word.explanationLanguages;
    }
    
    return result;
}

-(void)setData:(NSArray*)aData
{
    [data release];
    data = [aData retain];
    needsReindex = YES;
}

+(NSArray*)allWordLists:(NSString *)path
{
    if(!allLists)
    {
        // the return value
        NSMutableArray *lists = [[[NSMutableArray alloc] init] autorelease];
        //the individual word list
        VOCWordList *list = nil;
        
        //find out all valid resources
        for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]) {
            list = [NSKeyedUnarchiver unarchiveObjectWithFile:[path stringByAppendingPathComponent:file]];
            if(list)
            {
                if(!list.name)
                {
                    list.name = [file lastPathComponent];
                }
                [lists addObject:list];
            }
            
        }
        
        allLists = [lists copy]; 
    }
    
    return allLists;
}


-(id)initWithCoder:(NSCoder *)encoder
{
    self = [super init];
    
    if(self)
    {
        self.name = [encoder decodeObjectForKey:@"listName"];          
        self.data = [encoder decodeObjectForKey:@"data"];
        index = [[encoder decodeObjectForKey:@"index"] retain]; 
        
        if(self.index)
        {
            needsReindex = NO;
        }
        else
        {
            needsReindex = YES;
        }
   }
    
    NSLog(@"list`s init method has been called");
    
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:self.name forKey:@"listName"];
    [encoder encodeObject:self.data forKey:@"data"];
    [encoder encodeObject:self.index forKey:@"index"];
}

-(id)initWithName:(NSString*)aName withData:(NSArray *)aData
{
    self = [super init];
    if(self)
    {
        self.name = aName;
        self.data = aData;
        needsReindex = YES;
    }
    return self;
}

-(void)dealloc
{
    [self.name release];
    [self.data release];
    [self.index release];
    [super dealloc];
}

-(NSArray*)findWordsWithString:(NSString*)aString caseSentitive:(BOOL)caseSentitive
{
    NSMutableArray *result = [[[NSMutableArray alloc] init] autorelease];
    
    NSRange range = NSMakeRange(NSNotFound, 0);
    for (VOCWordData *word in self.data) {
        if(caseSentitive)
        {
            range = [word.word rangeOfString:aString];
        }
        else
        {
            range = [word.word rangeOfString:aString options:NSCaseInsensitiveSearch];
        }

        if(range.location == 0)
        {
            [result addObject:word];
        }
    }
    
    return [[result copy] autorelease];
}

-(BOOL)containsWord:(VOCWordData*)word
{
    return [self.data containsObject:word];
}

@end
